import serial


class MySerial:
    def __init__(self, port, baudrate, timeout):
        self.port = serial.Serial(port, baudrate)
        if self.port.is_open:
            print("open :", self.port.portstr)
        else:
            print("打开端口失败")
        self.recv_msg = ""

    def receive_msg(self):
        while True:
            size = self.port.in_waiting
            if size:
                self.recv_msg = self.port.read_all()
                print(f'收到串口数据:[{self.recv_msg.hex()}]')

    def send_msg(self, data):
        bytes_written = self.port.write(data)
        print(f"成功写入 {bytes_written} 字节:[{data.hex()}]到串口")
